<script type="text/javascript">
    function showTheWinner(){
//        var d = jQuery("#chosen_day").val();
        $.post("?action=getwinner", {}, function(r){
           $.blockUI({
              message:r             
           });
//           $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
        });
    }
    
    function showAnswerInfo(aid){
        $.post("?action=getwinner", {aid:aid}, function(r){
           $.blockUI({
              message:r             
           });
        });
    }
    
    function showCSVData()
    {
        var filter = jQuery("#filter").val();
        var url = "?action=exportcsv&filter=" + filter;        
        window.open(url, '_blank');
        window.focus();
    }
    
    function clearAllUserData()
    {
        var c = confirm("Are you sure to delete all of these records?");
        if(c){
            $.blockUI({message: "Deleting...",
                css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .5,
                color: '#fff'
            }});
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
            $.get("?action=deletealldata&rand" + Math.random(), {}, function(r){
                $.unblockUI();
                location.reload();
            });
        }        
    }
    
    function clearDupplicatedUserData(){
        var c = confirm("Are you sure you want to delete all duplicate entries?");
        if(c){
            $.blockUI({message: "Deleting...",
                css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .5,
                color: '#fff'
            }});
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
            $.get("?action=deletedupplicateddata&rand" + Math.random(), {}, function(r){
                $.unblockUI();
                alert(r + " duplicates removed");
                if(r != "0"){
                    location.reload();
                }                
            });
        }
    }
</script>

<div class="row-fluid no-margin">
	<div class="span12">
		<ul class="quicktasks">
			<li>
				<a href="javascript:showTheWinner();"> <img
						src="<?php echo $view->layoutUrl;?>assets/img/icons/essen/32/user.png"
						alt=""> <span>Pick Winner</span>
				</a>
			</li>			
                        <li>
				<a href="javascript:showCSVData();"> <img
						src="<?php echo $view->layoutUrl;?>assets/img/icons/essen/32/billing.png"
						alt=""> <span>Export to CSV</span>
				</a>
			</li>
                        <li>
				<a href="javascript:clearAllUserData();"> <img
						src="<?php echo $view->layoutUrl;?>assets/img/icons/essen/32/delete.png"
						alt=""> <span>Clear Data</span>
				</a>
			</li>
                        <li>
				<a href="javascript:clearDupplicatedUserData();"> <img
						src="<?php echo $view->layoutUrl;?>assets/img/icons/essen/32/delete-dup.png"
						alt=""> <span>Clear Duplicates</span>
				</a>
			</li>
		</ul>
            <form style="float: right; margin-top: -66px;" class="form-horizontal" id="formfilter" method="get" action="?action=viewresults">
                <input type="hidden" name="action" value="<?php echo $view->action;?>" />
                <input type="hidden" name="p" value="1" />
                
                <div class="control-group" style="float:left;margin-right:5px;">
                        <label class="control-label" for="select"></label>
                        <div class="controls">
                                <select id="filter" name="filter">
                                        <option value="all">---- All ----</option>                                        
                                        <option<?php if($view->filter == "today"){?> selected="selected" <?php } ?> value="today" >Today</option>
                                        <option<?php if($view->filter == "yesterday"){?> selected="selected" <?php } ?> value="yesterday" >Yesterday</option>
                                        <option<?php if($view->filter == "thisweek"){?> selected="selected" <?php } ?> value="thisweek" >This week</option>
                                        <option<?php if($view->filter == "lastweek"){?> selected="selected" <?php } ?> value="lastweek" >Last week</option>
                                        <option<?php if($view->filter == "thismonth"){?> selected="selected" <?php } ?> value="thismonth" >This month</option>
                                        <option<?php if($view->filter == "lastmonth"){?> selected="selected" <?php } ?> value="lastmonth" >Last month</option>                                        
                                </select>
                        </div>
                </div>
                
                <button type="submit" class="btn btn-primary">Filter</button>
                
            </form>
	</div>
</div>

<div class="row-fluid">
	<div class="span12">
		<div class="box">
			<div class="box-head">
				<h3>Reporting</h3>
			</div>
			<div class="box-content box-nomargin">				
				<table
					class='table table-striped table-media table-bordered<?php if($view->total <= 2000) { ?> dataTable<?php } ?>'>
					<thead>
						<tr>	
                                                        <th style="display:none;">ID</th>							
							<th>Name</th>
							<th>Email</th>
							<th>IP</th>	
                                                        <th>Newsletter</th>
							<th>Date/time</th>
                                                        <th>Action</th>
						</tr>
					</thead>
					<tbody>
                                            
                                        <?php foreach($view->answers as $i => $u) {                                             
                                            ?>
                                        <tr<?php if($i % 2 != 0){?> style='background-color:#F6F6F6;'<?php } ?>>
                                            <td style="display:none;"><?php echo 10000000 - $u['answer_id'];?></td>                                            
                                            <td><?php echo $u['name'];?></td>
                                            <td><?php echo $u['email'];?></td>
                                            <td><?php echo $u['ip'];?></td>                                         
                                            <td style="text-align: center;"><?php if($u['is_newsletter'] == "on"){?><span style="color:green;font-weight: bold;">Yes</span><?php } else { ?>No<?php } ?></td>   
                                            <td><?php echo date("d.m.Y H:i", $u['update_time']);?></td>
                                            <td class='actions_big'>
                                                    <div class="btn-group">                                                
                                                        <center>
                                                        <a href="javascript:showAnswerInfo(<?php echo $u['answer_id']; ?>)" class='btn btn-mini tip' title="Show answer detail">
                                                                <img src="<?php echo $view->layoutUrl; ?>assets/img/icons/fugue/information.png" alt="">
                                                        </a>                                            
                                                        </center>
                                                    </div>
                                            </td>
                                        </tr>
                                        <?php } ?>                                            					
					</tbody>
				</table>
                            
                            <?php if($view->total > 2000) { ?>
                            <div class="dataTables_info" id="DataTables_Table_0_info">
                                Showing page <?php echo $view->currentPage;?>/<?php echo $view->numOfPages;?>
                            </div>
                            
                            <div class="dataTables_paginate paging_bootstrap pagination">
                                <ul>
                                    <li class="prev<?php if($view->prePage == $view->currentPage){ ?> disabled<?php } ?>">
                                        <a href="?action=viewresults&p=<?php echo $view->prePage;?>&day=<?php echo $view->day;?>">← Previous</a>
                                    </li>
                                    <?php for($i = $view->startpage; $i< $view->endpage;$i++) {?>
                                    <li<?php if($i == $view->currentPage){?> class="active"<?php } ?>>
                                        <a href="?action=viewresults&p=<?php echo $i;?>&day=<?php echo $view->day;?>"><?php echo $i;?></a>
                                    </li>
                                    <?php } ?>                                    
                                    <li class="next<?php if($view->nextPage == $view->currentPage){ ?> disabled<?php } ?>">
                                        <a href="?action=viewresults&p=<?php echo $view->nextPage;?>&day=<?php echo $view->day;?>">Next → </a>
                                    </li>
                                </ul>
                            </div>
                            <?php } ?>
                            
			</div>
		</div>
	</div>
</div>
